package net.wanho.controller;

import net.wanho.client.UserClient;
import net.wanho.entity.User;
import net.wanho.vo.AjaxResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * Author：汤小洋
 * Date：2023-08-02 9:57
 * Description：<描述>
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Resource
    // private RestTemplate restTemplate; // 发送HTTP请求
    private UserClient userClient;

    @GetMapping("/test")
    public String test(){
        // 直接调用服务提供者
        // String str = restTemplate.getForObject("http://localhost:6001/users/test", String.class);
        
        // 通过服务中心调用服务提供者
        String str = userClient.test();

        return "consumer--->"  + str;
    }

    @GetMapping
    public AjaxResult list(String username){
        return userClient.list(username);
    }

    @DeleteMapping("/{id}")
    public AjaxResult removeById(@PathVariable int id){
        return userClient.removeById(id);
    }

    @PostMapping
    public AjaxResult add(@RequestBody User user){
        return userClient.add(user);
    }

    @PutMapping
    public AjaxResult modify(@RequestBody User user){
        return userClient.modify(user);
    }

    @GetMapping("/query")
    public AjaxResult query(@RequestParam Map<String,Object> map){
        // Map<String,Object> map = new HashMap<>();
        // map.put("username",user.getUsername());
        // map.put("password",user.getPassword());
        return userClient.query(map);
    }

}
